package com.czh.zhiwen.scoring;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.czh.zhiwen.model.dto.question.QuestionContentDTO;
import com.czh.zhiwen.model.entity.App;
import com.czh.zhiwen.model.entity.Question;
import com.czh.zhiwen.model.entity.ScoringResult;
import com.czh.zhiwen.model.entity.UserAnswer;
import com.czh.zhiwen.model.vo.QuestionVO;
import com.czh.zhiwen.service.QuestionService;
import com.czh.zhiwen.service.ScoringResultService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@ScoringStrategyConfig(appType = 0,scoringStrategy = 0)
public class MarkScoringStrategyImpl implements ScoringStrategy {

    @Resource
    private QuestionService questionService;


    @Resource
    private ScoringResultService scoringResultService;


    @Override
    public UserAnswer doScore(List<String> choices, App app) throws Exception {
        // 1、根据id查询题目和题目的答案
        Long appid = app.getId();
        Question question = questionService.getOne(
                Wrappers.lambdaQuery(Question.class)
                        .eq(Question::getAppId, app.getId()));

        // 2、根据AppId获取用户的答案列表
        List<ScoringResult> list = scoringResultService.list(
                Wrappers.lambdaQuery(ScoringResult.class)
                        .eq(ScoringResult::getAppId, appid)
                        .orderByDesc(ScoringResult::getResultScoreRange)
        );
        // 3、根据用户选择的答案和题目的答案来进行评分
        QuestionVO questionVO = QuestionVO.objToVo(question);
        List<QuestionContentDTO> questionContent = questionVO.getQuestionContent();
        //统计每个选项被选择的次数
        int resultScore = 0;
        //遍历题目内容
        for (QuestionContentDTO questionContentDTO : questionContent) {
            //遍历用户的答案
            for (String answer : choices) {
                //遍历题目的选项
                for(QuestionContentDTO.Option option: questionContentDTO.getOptions()){
                    //如果用户选择的答案和题目的答案一致
                    if(option.getKey().equals(answer)){
                        int score = Optional.of(option.getScore()).orElse(0);
                        resultScore += score;
                    }
                }
            }
        }
        ScoringResult maxScoringResult = list.get(0);
        for (ScoringResult result : list) {
            if(resultScore >= result.getResultScoreRange()){
                maxScoringResult = result;
                break;
            }

        }
        //返回评分结果
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setAppId(app.getId());
        userAnswer.setAppType(app.getAppType());
        userAnswer.setScoringStrategy(app.getScoringStrategy());
        userAnswer.setChoices(JSONUtil.toJsonStr(choices));
        userAnswer.setResultId(maxScoringResult.getId());
        userAnswer.setResultName(maxScoringResult.getResultName());
        userAnswer.setResultDesc(maxScoringResult.getResultDesc());
        userAnswer.setResultPicture(maxScoringResult.getResultPicture());
        userAnswer.setResultScore(resultScore);

        return userAnswer;
    }
}
